import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Vector;

import Framework.*;



public class win_Playlist extends pnl_Ancestor implements ActionListener{

	btn_TopButtons 		b_TopButtons 	= new btn_TopButtons("Playlist");
	btn_BottomButtons 	b_BottomButtons = new btn_BottomButtons(6);
	
	// Setting up scrollable, first Rectangle is the actual window dimensions, the second is the 'virtual' dimensions. I don't set
	// Virtual here, because I do it in win_Playlist instantiation.
	int			   	pnl_PlaylistWidth 		= 300;
	pnl_Scrollable 	pnl_Playlist			= new pnl_Scrollable(new Rectangle(0,0,pnl_PlaylistWidth,1600), new Rectangle(0,0,0,0), false);
		
	btn_Ancestor btn_btm_Audio 			= new btn_Ancestor("Audio");
	btn_Ancestor btn_btm_SavePlaylist 	= new btn_Ancestor("Save Playlist");
	btn_Ancestor btn_btm_LoadPlaylist 	= new btn_Ancestor("Load Playlist");
	btn_Ancestor btn_btm_Movies 			= new btn_Ancestor("Movies");
	btn_Ancestor btn_btm_Library 			= new btn_Ancestor("Library");
	btn_Ancestor btn_OpenMediaWindow 		= new btn_Ancestor("Music");
	btn_Ancestor btn_OpenNavWindow		= new btn_Ancestor("Navigation");

	JLabel lbl_Track 	= new JLabel("Stub");
	JLabel lbl_Artist 	= new JLabel("Stub");
	JLabel lbl_Album 	= new JLabel("Stub");
	
	dlg_Keyboard	dlg_SavePlaylist = new dlg_Keyboard("Save As: ", false);
	dlg_Keyboard	dlg_LoadPlaylist = new dlg_Keyboard("Open: ", false);

	
	
	

	
	
	public win_Playlist(){
		super(); // Remember to always call super on JPanel_Anc constructors!
		
		// Setting up labels positions and adding to panel
		lbl_Track.setBounds(pnl_PlaylistWidth + 50, 150, pnl_PlaylistWidth, 50);
		lbl_Artist.setBounds(pnl_PlaylistWidth + 50, 260, pnl_PlaylistWidth, 50);
		lbl_Album.setBounds(pnl_PlaylistWidth + 50, 320, pnl_PlaylistWidth, 50);
		this.add(lbl_Track);
		this.add(lbl_Artist);
		this.add(lbl_Album);
		
		
		// Generating random data for metadata
		changeMediaInfo();
		
		// Iterating through playlist, Adding each as a button to pnl_Scrollable.. 
		java.util.Iterator<String> itr = createPlaylist().iterator();
		
		btn_Ancestor 			lbtn_playlistButtons;
		int li_yCoord 		= 0;
		int li_Spacer 		= 10; // Pixels between each button
		int li_buttonHeight = 60;
		
		while (itr.hasNext()){
			// Creating the button, setting size
			lbtn_playlistButtons = new btn_Ancestor(itr.next());
			lbtn_playlistButtons.setBounds(10, li_yCoord, pnl_PlaylistWidth, li_buttonHeight);
			
			// Adding event listener. As we don't really care which button is pressed, we just check
			// event to make sure that the parent frame is not this frame. Otherwise, We'd need to 
			// keep a reference to this button, and use e.getSource().equals(buttonReference);
			lbtn_playlistButtons.addActionListener(this);
			
			// Adding it to the playlist panel
			pnl_Playlist.add(lbtn_playlistButtons);
			
			// Incrementing position.
			li_yCoord = li_yCoord + li_buttonHeight + li_Spacer;
		}
		
		pnl_Playlist.setInternalFrameSize(new Rectangle(0,0,pnl_PlaylistWidth, li_yCoord));
		
		pnl_Playlist.i_InternalFrame.setBackgroundImage(this.backgroundImagePath);
		pnl_Playlist.i_InternalFrame.setBackgroundFloating(true, new Rectangle(0,0,800,600));
		
		this.add(pnl_Playlist);
		
		// Adding top and bottom buttons. This should go last!
		// Adding bottom buttons to bottom Button frame
		b_BottomButtons.add(btn_btm_Audio, 0);
		b_BottomButtons.add(btn_btm_Library, 1);
		b_BottomButtons.add(btn_btm_LoadPlaylist, 3);
		b_BottomButtons.add(btn_btm_SavePlaylist, 4);
		b_BottomButtons.add(btn_btm_Movies, 5);
		
		btn_btm_SavePlaylist.addActionListener(this);
		btn_btm_LoadPlaylist.addActionListener(this);
		btn_btm_Library.addActionListener(this);
		
		// Adding top and bottom frames to this frame
		this.add(b_TopButtons);
		this.add(b_BottomButtons);
		
		// Call validate on completion of window. Otherwise some things may not render.
		this.validate();
		this.repaint();
		
		// Adding some filler into the open dialogue
		dlg_LoadPlaylist.addSuggestions();
		
	}	
	
	
	
	public void actionPerformed(ActionEvent e) {
		
		Object actionSource = (btn_Ancestor) e.getSource();
		
		if ( pnl_Playlist.equals(((Component) actionSource).getParent()) ){
			changeMediaInfo();
		} else if (actionSource.equals(btn_btm_SavePlaylist) ){
			dlg_SavePlaylist.activate(this);
		} else if (actionSource.equals(btn_btm_LoadPlaylist)) {
			dlg_LoadPlaylist.activate(this);
		} else if (actionSource.equals(btn_btm_Library)) {
			enterSubMenu(TeamRocket.w_MediaLibrary);			
		}
		
		
	}
	
	
	// Stop reading here. The rest is just generation for track / album names etc.
	
	
	private Vector<String> createPlaylist(){
		// Creating a list of tracks titles / artists / albums
		Vector <String> ls_TrackList = new Vector<String>();
		String ls_Artist;
		String ls_Track;
		for (int i = 0; i < 50; i++){
			if (i < 10){
				ls_Artist = "Armin Van Buuren";
			} else if (i < 20){
				ls_Artist = "Tiesto";
			} else {
				ls_Artist = "Paul Oakenfold";
			}
			if (i % 3 == 0){
				ls_Track = "Get up and Dance!";
			} else if (i % 4 == 0){
				ls_Track = "Do something cool";
			} else if (i % 2 == 0){
				ls_Track = "Lets party";
			} else {
				ls_Track = "I have no friends.";
			}
			ls_TrackList.add(ls_Artist + " - " + ls_Track);
		}
		return ls_TrackList;
	}
	
	public void changeMediaInfo(){
		// Setting up media INformation for main pane.
		int i = new Random().nextInt(50);
		
		if (i < 10){ 
			lbl_Artist.setText("Armin Van Buuren"); 
		}
		else if (i < 20){ 
			lbl_Artist.setText("Tiesto"); 
		}
		else {
			lbl_Artist.setText("Paul Oakenfold"); 
		}
		
		if (i % 3 == 0) {
			lbl_Album.setText("Self Titled"); 
		}
		else {
			lbl_Album.setText("Party Anthems"); 
		}		
		if (i % 3 == 0){ 
			lbl_Track.setText("Get up and Dance!"); 
		} 
		else if (i % 4 == 0){
			lbl_Track.setText("Do something cool");
		} 
		else if (i % 2 == 0){
			lbl_Track.setText("Lets party"); 
		} 
		else {
			lbl_Track.setText("I have no friends."); 
		}		
		this.validate();
		this.repaint();
	}
	
}
